import java.io.IOException;
import java.util.Scanner;

public class CinemaSystem {
    private static final String DATA_FILE = "customers.dat";
    private static final String FILM_SESSION_FILE = "films_sessions.dat";
    private static final String TICKET_FILE = "tickets.dat";
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Customer.loadCustomersFromFile(DATA_FILE);
        Plan.loadStaticData(FILM_SESSION_FILE);
        Sale.loadStaticData(TICKET_FILE);
        Manager admin = Manager.getAdmin();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                Customer.saveCustomersToFile(DATA_FILE);
                Plan.saveStaticData(FILM_SESSION_FILE); // 修正参数顺序
                Sale.saveStaticData(TICKET_FILE); // 修正参数顺序
            } catch (IOException e) {
                System.out.println("保存数据失败: " + e.getMessage());
            }
            System.out.println("程序已关闭，数据已保存");
        }));
        Scanner scanner = new Scanner(System.in);
        Customer customer=null;
        do {
            System.out.println("请选择登录类型：");
            System.out.println("1.管理员\n2.经理\n3.前台\n4.退出系统");
            int choice = scanner.nextInt();
            scanner.nextLine();
            if (choice == 4) { // 先判断退出，避免后续输入
                return;
            }
            System.out.println("ID:");
            int id = scanner.nextInt();
            scanner.nextLine();
            System.out.println("password:");
            String password = scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.println("Manager login");
                    customer = admin.login(id, password);
                    break;
                case 2:
                    System.out.println("Director login");
                    customer = Director.login(id, password);
                    break;
                case 3:
                    System.out.println("Stage login");
                    customer = Stage.login(id, password);
                    break;
                default:
                    System.out.println("未找到所输入的用户，请重新输入");
                    customer = null;
            }


            while (customer != null && customer.pwd) {
                int choice1 = 0;
                switch (choice) {
                    case 1:
                        Manager manager = (Manager) customer;
                        if(!manager.getOnce()){
                            System.out.println("请首次设置密码：");
                            String password1 = scanner.nextLine();
                            manager.alterSelfPassword(password1);
                        }
                        System.out.println("请选择你要什么服务：");
                        System.out.println("1.修改自身密码\n2.重置经理或前台密码\n3.列出所有用户信息\n4.删除指定用户信息\n5.查询用户信息\n6.增添用户信息\n7.修改用户信息\n8.退出登录");
                        choice1 = scanner.nextInt();
                        switch (choice1) {
                            case 1:
                                System.out.println("请输入新密码：");
                                scanner.nextLine();
                                String password1 = scanner.nextLine();
                                manager.alterSelfPassword(password1);
                                break;
                            case 2:
                                System.out.println("请输入要重置用户的ID:");
                                int Id=scanner.nextInt();
                                manager.resetOtherPassword(Id);
                                break;
                            case 3:
                                manager.showPeople();
                                break;
                            case 4:
                                manager.removeCustomer(new Scanner(System.in));
                                break;
                            case 5:
                                manager.searchPeople(new Scanner(System.in));
                                break;
                            case 6:
                                manager.addPeople(new Scanner(System.in));
                                break;
                            case 7:
                                manager.changeMassage(new Scanner(System.in));
                                break;
                            case 8:
                                manager.logout();
                                break;
                        }
                        break;
                    case 2:
                        Director director = (Director) customer;
                        if(!director.getOnce()){
                            System.out.println("请首次设置密码：");
                            String password1 = scanner.nextLine();
                            director.changeSelfPassword(password1);
                        }
                        System.out.println("请选择你要什么服务：");
                        System.out.println("1.修改自身密码\n2.重置前台密码\n3.列出所有影片信息\n4.删除指定影片信息\n5.查询影片信息\n6.增添影片信息\n7.修改影片信息\n8.增加场次\n9.修改场次\n10.删除场次\n11.自动排片\n12.列出所有场次信息\n13.查看当日场次数据\n14.查看当周或当月场次数据\n15.退出登录");
                        choice1 = scanner.nextInt();
                        switch (choice1) {
                            case 1:
                                System.out.println("请输入新密码：");
                                scanner.nextLine();
                                String password1 = scanner.nextLine();
                                director.changeSelfPassword(password1);
                                break;
                            case 2:
                                director.resetOtherPassword(new Scanner(System.in));
                                break;
                            case 3:
                                director.showFilm();
                                break;
                            case 4:
                                director.deleteFilm(new Scanner(System.in));
                                break;
                            case 5:
                                director.searchFilm(new Scanner(System.in));
                                break;
                            case 6:
                                director.addFilm(new Scanner(System.in));
                                break;
                            case 7:
                                director.changeFilm(new Scanner(System.in));
                                break;
                            case 8:
                                director.addSession(new Scanner(System.in));
                                break;
                            case 9:
                                director.changeSession(new Scanner(System.in));
                                break;
                            case 10:
                                director.deleteSession(new Scanner(System.in));
                                break;
                            case 11:
                                director.arrangeSession();
                                break;
                            case 12:
                                director.showSession(new Scanner(System.in));
                                break;
                            case 13:
                                director.searchDayTicket();
                                break;
                            case 14:
                                director.searchWeekTicket(new Scanner(System.in));
                                break;
                            case 15:
                                director.logout();
                                break;
                            default:
                                System.out.println("error");
                        }
                        break;
                    case 3:
                        Stage stage = (Stage) customer;
                        if(!stage.getOnce()){
                            stage.setOnce(true);
                            System.out.println("请首次设置密码：");
                            String password1 = scanner.nextLine();
                            stage.changeSelfPassword(password1);
                        }
                        System.out.println("请选择你要什么服务：");
                        System.out.println("1.退票\n2.售票\n3.列出所有上映影片信息\n4.列出所有场次信息\n5.列出指定影片和场次信息\n6.退出登录");
                        choice1 = scanner.nextInt();
                        switch (choice1) {
                            case 1:
                                stage.deleteChairs(new Scanner(System.in));
                                break;
                            case 2:
                                stage.chooseChairs(new Scanner(System.in));
                                break;
                            case 3:
                                stage.showFilm();
                                break;
                            case 4:
                                stage.showSession();
                                break;
                            case 5:
                                stage.showChair(new Scanner(System.in));
                                break;
                            case 6:
                                stage.logout();
                                break;
                            default:
                                System.out.println("error");
                        }
                        break;
                    default:
                        System.out.println("error");
                }
            }

        } while (true);
    }
}
